package com.ptmall.app.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ptmall.app.MainActivity;
import com.ptmall.app.R;
import com.ptmall.app.bean.AddressBean;
import com.ptmall.app.bean.ProductBean;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.GoodDetilActivity;
import com.ptmall.app.ui.activity.RqActivity;
import com.ptmall.app.utils.GlideUtil;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2018/1/12.
 */

public class TimeShopAdapter extends BaseAdapter {

    private Context mContext;
    private List<ProductBean> mDataList;
    private LayoutInflater mLayoutInflater;

    public TimeShopAdapter(Context context, List<ProductBean> list) {
        this.mContext = context;
        this.mDataList = list;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void pushData(List<ProductBean> list){
        this.mDataList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mDataList == null){
            return 0;
        }
        return mDataList.size();
    }


    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.item_timeshop,  parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ProductBean bean = mDataList.get(position);

//        GlideUtil.showImg(mContext,bean.list_img,viewHolder.goodimg);
//        viewHolder.goodname.setText(bean.product_name);
//        viewHolder.goodprice.setText("￥"+bean.sell_price);
//        viewHolder.goodnum.setText("仅剩"+new BigDecimal(bean.xian_num).subtract(new BigDecimal(bean.qiang_num)).setScale(0).toPlainString()
//        +"份");
//        if(new BigDecimal(bean.qiang_num).intValue() == new BigDecimal(bean.xian_num).intValue()){
//            viewHolder.isnogood.setVisibility(View.VISIBLE);
//            viewHolder.goodnum.setText(bean.xian_num+"份已抢光");
//            viewHolder.numseek.setVisibility(View.GONE);
//            viewHolder.addcar.setVisibility(View.GONE);
//            viewHolder.more.setVisibility(View.VISIBLE);
//        }else {
//            viewHolder.isnogood.setVisibility(View.GONE);
//            viewHolder.more.setVisibility(View.GONE);
//            viewHolder.addcar.setVisibility(View.VISIBLE);
//        }
//        viewHolder.numseek.setProgress(new BigDecimal(bean.xian_num).intValue());
//
//        viewHolder.goodimg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(mContext, RqActivity.class);
//                mContext.startActivity(intent);
//            }
//        });
//        viewHolder.addcar.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                Intent intent = new Intent(mContext, GoodDetilActivity.class);
////                intent.putExtra("id",bean.productId);
////                mContext.startActivity(intent);
//                Map<String, String> map = new HashMap<>();
//                map.put("productId", bean.productId);
//                map.put("num", "1");
//                ApiDataRepository.getInstance().addShopCar(map, new ApiNetResponse<Object>(mContext) {
//                    @Override
//                    public void onSuccess(Object data) {
//                        Toast.makeText(mContext,"加入购物车成功",Toast.LENGTH_SHORT).show();
//                    }
//                });
//            }
//        });
//        viewHolder.more.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(mContext, MainActivity.class);
//                mContext.startActivity(intent);
//            }
//        });
        return convertView;
    }

    class ViewHolder {

        TextView goodnum,goodprice,goodname,isnogood;
        Button more;
        ImageView goodimg;
        ProgressBar numseek;
        LinearLayout addcar;

        ViewHolder(View view) {
            goodnum = (TextView) view.findViewById(R.id.goodnum);
            numseek = (ProgressBar) view.findViewById(R.id.numseek);
            addcar = (LinearLayout) view.findViewById(R.id.addcar);
            goodprice = (TextView) view.findViewById(R.id.goodprice);
            goodname = (TextView) view.findViewById(R.id.goodname);
            isnogood = (TextView) view.findViewById(R.id.isnogood);
            goodimg = (ImageView) view.findViewById(R.id.goodimg);
            more = (Button) view.findViewById(R.id.more);
        }
    }

}
